home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / wtjmarch.zip / SLIDE.ZIP / SLIDING.CPP next >
C/C++ Source or Header  |  1991-12-15  |  4KB  |  160 lines

  1. // sliding.cpp
  2. #include"stdwin.h"
  3. #include<stdio.h>
  4. #include<string.h>
  5.  
  6. const UP = 1;
  7. const DOWN = 0;
  8. const WM_STARTIMER = WM_USER;
  9. const WM_STOPTIMER = WM_USER+1;
  10. const FREQ_DEFAULT = 50;
  11.  
  12. char *winTitle = "Sliding Text";
  13.  
  14. class SlidingText : public WinAppStdWindow
  15.     {
  16.     char *message;
  17.     BOOL direction, adjusted;
  18.     int LineHeight, ScreenHeight, curY;
  19.     WORD frequency;
  20.  
  21. public:
  22.     SlidingText(char *winname, char *msg) : WinAppStdWindow(winname)
  23.         {
  24.             // insert calls to change window class info here
  25.         message = msg;
  26.         direction = DOWN;
  27.         LineHeight = ScreenHeight = curY = 0;
  28.         adjusted = FALSE;
  29.         frequency = FREQ_DEFAULT;
  30.         }
  31.  
  32.     void WMPAINT(void)
  33.         {
  34.         PAINTSTRUCT ps;
  35.         BeginPaint(hWnd, &ps);
  36.         TEXTMETRIC tmFontInfo;
  37.         GetTextMetrics(ps.hdc, (LPTEXTMETRIC) &tmFontInfo);
  38.  
  39.         LineHeight = tmFontInfo.tmExternalLeading + tmFontInfo.tmHeight;
  40.         if(!adjusted)
  41.             {
  42.             ScreenHeight -= LineHeight;
  43.             adjusted = TRUE;
  44.             }
  45.  
  46.             // set alignment flags for centering
  47.         SetTextAlign(ps.hdc, TA_CENTER);    
  48.  
  49.         RECT rect;
  50.  
  51.         GetClientRect(hWnd,&rect);   // new centered rectangle
  52.         TextOut(ps.hdc,rect.right/2,curY,message, strlen(message));
  53.  
  54.         EndPaint(hWnd, &ps);
  55.         }
  56.  
  57.     void WMTIMER(void)
  58.         {
  59.         if(curY == ScreenHeight)
  60.             direction = UP;
  61.         if(curY == 0)
  62.             direction = DOWN;
  63.  
  64.         if(direction == DOWN)
  65.             {
  66.             ScrollWindow(hWnd,0,1,NULL,NULL);
  67.             curY++;
  68.             }
  69.         else
  70.             {
  71.             ScrollWindow(hWnd,0,-1,NULL,NULL);
  72.             curY--;
  73.             }
  74.         UpdateWindow(hWnd);
  75.         }
  76.  
  77.     void WMCREATE(void)         
  78.         {   
  79.         StartTimer();               
  80.         SetTitle();
  81.         }
  82.     void WMSIZE(void)
  83.         {
  84.         ScreenHeight = (HIWORD(lParam) - LineHeight);
  85.         curY = 0;
  86.         }
  87.     void WMCLOSE(void)          {   DestroyWindow(hWnd);        }
  88.     void WMDESTROY(void)        {   PostQuitMessage(0);         }
  89.  
  90.         // Accept LeftButton and '+' for faster (more frequent) action
  91.         // and RightButton and '-' for slower (less frequent) action
  92.     void WMLBUTTONDOWN(void)            {   Faster();   }
  93.     void WMRBUTTONDOWN(void)            {   Slower();   }
  94.     void WMCHAR(void)
  95.         {
  96.         if(wParam == '+')
  97.             Faster();
  98.         if(wParam == '-')
  99.             Slower();
  100.         }
  101.     void Faster(void)
  102.         {
  103.         PostMessage(hWnd,WM_STOPTIMER,0,0L);
  104.         if(frequency > 0)
  105.             frequency -= 10;
  106.         PostMessage(hWnd,WM_STARTIMER,0,0L);
  107.         SetTitle();
  108.         }
  109.     void Slower(void)
  110.         {
  111.         PostMessage(hWnd,WM_STOPTIMER,0,0L);
  112.         if(frequency < 65535u)
  113.             frequency += 10;
  114.         PostMessage(hWnd,WM_STARTIMER,0,0L);
  115.         SetTitle();
  116.         }
  117.     void SetTitle(void)
  118.         {
  119.         char temp[50];
  120.         sprintf(temp,"%s  (Speed = %05u)",winTitle,~frequency);
  121.         SetWindowText(hWnd, temp);
  122.         }
  123.     void UserMessages(void);
  124.     void WMSTARTTIMER(void)
  125.         {
  126.         StartTimer();
  127.         UserMessageUsed = TRUE;
  128.         }
  129.     void WMSTOPTIMER(void)
  130.         {
  131.         StopTimer();
  132.         UserMessageUsed = TRUE;
  133.         }
  134.     void StartTimer(void)       {   SetTimer(hWnd, 1, frequency, NULL); }
  135.     void StopTimer(void)        {   KillTimer(hWnd,1);                  }
  136.     };
  137.  
  138. void SlidingText::UserMessages(void)
  139.     {
  140.     switch(msg)
  141.         {
  142.         case WM_STARTIMER:
  143.             WMSTARTTIMER();
  144.             return;
  145.         case WM_STOPTIMER:
  146.             WMSTOPTIMER();
  147.             return;
  148.         }
  149.     }
  150.  
  151. int PASCAL WinMain(HANDLE, HANDLE, LPSTR, int)
  152.     {                                   // initialize the object
  153.     SlidingText MyWin(winTitle, "Hello Windows from C++");
  154.     MyWin.Display();                    // open the window
  155.     return MyWin.Run();                 // process any messages
  156.     }
  157.  
  158.  
  159.  
  160.